package com.jiang.demo.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiang.demo.user.domain.Book;
import com.jiang.demo.user.mapper.BookMapper;
import com.jiang.demo.user.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;
    @Override
    public List<Book> info() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        List<Book> books = bookMapper.selectList(queryWrapper);
        return books;
    }

    @Override
    public List<Book> gettype(Map<String, String> data) {
        Integer type = Integer.valueOf(data.get("type"));
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<Book> books = bookMapper.selectList(queryWrapper);

        return books;
    }

    @Override
    public Map<String, String> updata(Map<String, String> data) {
        Integer number = Integer.valueOf(data.get("number"));
        Integer id = Integer.valueOf(data.get("id"));
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Book book1 = bookMapper.selectOne(queryWrapper);

        Book book = new Book(
                book1.getId(),
                book1.getName(),
                book1.getPhoto(),
                book1.getType(),
                number,
                book1.getType()
        );

        bookMapper.updateById(book);
        Map<String,String> map = new HashMap<>();
        map.put("error_message","success");
        return map;
    }

    @Override
    public Map<String, String> add(Map<String, String> data) {
        String name = data.get("name");
        String photo = data.get("photo");
        Integer number = Integer.valueOf(data.get("number"));

        Integer type = Integer.valueOf(data.get("type"));
        Book book = new Book(
                null,
                name,
                photo,
                type,
                number,
                1
        );
        bookMapper.insert(book);
        Map<String,String> map = new HashMap<>();
        map.put("error_message","success");
        return map;
    }

    @Override
    public Map<String, String> delet(Map<String, String> data) {
        Integer id = Integer.valueOf(data.get("id"));
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Book book1 = bookMapper.selectOne(queryWrapper);

        Book book = new Book(
                book1.getId(),
                book1.getName(),
                book1.getPhoto(),
                book1.getType(),
                book1.getNumber(),
                0
        );

        bookMapper.updateById(book);
        Map<String,String> map = new HashMap<>();
        map.put("error_message","success");
        return map;
    }
}
